#include "compiler_runner.hpp"
#include "../cpp-httplib/httplib.h"

using namespace ns_complier_runner;
using namespace httplib;
void Usage(std::string proc)
{
    std::cout << "Usage\r\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    /*-------------------------------------------
                    http-lib
     ------------------------------------------*/
    httplib::Server serv;
    // serv.set_base_dir("./wwwroot");

    // serv.Get("/hello", [](const Request &req, Response &resp)
    //          { resp.set_content("just for test!!!", "text/plain; charset=utf-8"); });

    serv.Post("/compiler_runner", [](const Request &req, Response &resp)
              {
            std::string in_json = req.body;
            // std::cout << in_json << std::endl;
            std::string out_json;
            if(!in_json.empty())
            {
                complier_runner::Start(in_json, &out_json);
                resp.set_content(out_json, "application/json; charset=utf-8"); 
                // std::cout << out_json << std::endl;
            } });

    serv.listen("0.0.0.0", atoi(argv[1]));

    /*-------------------------------------------
                        测试用例
     ------------------------------------------*/
    // Json::Value in_value;
    // // R"()"; c++新特性, 保持原始字符串
    // in_value["code"] = R"(#include<iostream>
    // int main(){
    //     std::cout << "You can see me" << std::endl;
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpu_limit"] = 1;
    // in_value["mem_limit"] = 1024 * 30;
    // Json::FastWriter writer;
    // std::string in_json = writer.write(in_value);
    // std::cout << in_json << std::endl;

    // //这个是将来给客户端返回的json串
    // std::string out_json;
    // complier_runner::Start(in_json, &out_json);

    // std::cout << out_json << std::endl;
    return 0;
}